package com.kingwang.demo.springsecuritytokendemo.security.sso.authenticator.impl;

import com.kingwang.demo.springsecuritytokendemo.security.AuthenticationResultCode;
import com.kingwang.demo.springsecuritytokendemo.security.sso.SsoAuthenticationResult;
import com.kingwang.demo.springsecuritytokendemo.security.sso.SsoUserInfo;
import com.kingwang.demo.springsecuritytokendemo.security.sso.authenticator.SsoAuthenticator;

import javax.servlet.http.HttpServletRequest;

public class ShanDongSsoAuthenticator implements SsoAuthenticator {
    private String areaCode;

    private HttpServletRequest request;

    public ShanDongSsoAuthenticator(String areaCode, HttpServletRequest request) {
        this.areaCode = areaCode;
        this.request = request;
    }

    private String getRequestParameter(String name) {
        return this.request.getParameter(name);
    }

    @Override
    public SsoAuthenticationResult authenticate() {
        String token = getRequestParameter("token");

        SsoUserInfo userInfo = getUserInfoFromToken(token);

        boolean isValid = validateUser(userInfo);

        SsoAuthenticationResult result = new SsoAuthenticationResult();

        if (isValid) {
            result.setCode(AuthenticationResultCode.SUCCESS);
            result.setUsername(userInfo.getUsername());
            result.setDisplayName(userInfo.getDisplayName());
            result.setAreaCode(this.areaCode);

            result.addAttribute("custom-attribute-01", "aaa");
        } else {
            result.setCode(AuthenticationResultCode.FAILED);
        }

        return result;
    }

    private boolean validateUser(SsoUserInfo userInfo) {
        return true;
    }

    private SsoUserInfo getUserInfoFromToken(String token) {
        return null;
    }
}
